package com.huli.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huli.domain.Owner;
import com.huli.domain.OwnerIncome;
import com.huli.domain.OwnerVip;
import com.huli.mapper.OwnerIncomeMapper;
import com.huli.mapper.OwnerMapper;
import com.huli.mapper.OwnerVipMapper;
import com.huli.param.OwnerParam;
import com.huli.service.OwnerService;
import com.huli.utils.JwtUtils;
import com.huli.utils.TimeUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OwnerServiceImpl extends ServiceImpl<OwnerMapper, Owner> implements OwnerService {

    @Autowired
    private OwnerMapper ownerMapper;
    @Autowired
    private OwnerVipMapper ownerVipMapper;
    @Autowired
    @Qualifier("AAA")
    private OwnerIncomeMapper ownerIncomeMapper;
    @Autowired
    private JwtUtils jwtUtils;


    @Override
    public Owner selectOwnerById(OwnerParam ownerParam) {

        return ownerMapper.selectById(ownerParam.getOwnerId());

    }

    @Override
    public String login(OwnerParam ownerParam) {
        QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_account", ownerParam.getOwnerAccount())
                .eq("owner_password", ownerParam.getOwnerPassword());

        return jwtUtils.generateToken(ownerMapper.selectOne(queryWrapper).getOwnerId());
    }

    @Transactional
    @Override
    public String buyVip(OwnerParam ownerParam) {
        //查询会员表
        List<OwnerVip> ownerVips = ownerVipMapper.selectList(new QueryWrapper<>());
        System.out.println(ownerVips);
        //判断购买的哪个段位的会员
        OwnerVip vip = new OwnerVip();
        for (OwnerVip ownerVip : ownerVips) {
            if (ownerVip.getOwnerVipId().equals(ownerParam.getOwnerVipId())) {
                DateTime dateTime = new DateTime().plusMonths(3);
                System.out.println(dateTime);
                vip.setOwnerVipPrice(ownerVip.getOwnerVipPrice());
                vip.setOwnerVipTime(ownerVip.getOwnerVipTime());
            }
        }

        System.out.println(vip);
        //通过ID查询用户信息
        Owner owner1 = ownerMapper.selectById(ownerParam.getOwnerId());
        Owner owner = new Owner();
        BeanUtil.copyProperties(owner1, owner);

        //扣除余额          修改时间
        Date date = TimeUtils.addMinute(owner.getOwnerVipTime(), vip.getOwnerVipTime());
        owner.setOwnerMoney(owner1.getOwnerMoney().subtract(vip.getOwnerVipPrice()))
                .setOwnerVipTime(date);


        System.out.println("=================================================================================");
        System.out.println(owner);
        System.out.println("=================================================================================");
        UpdateWrapper<Owner> ownerUpdateWrapper = new UpdateWrapper<>();
        ownerUpdateWrapper.eq("owner_id", owner.getOwnerId());
        ownerMapper.update(owner, ownerUpdateWrapper);

        //加入收入表
        OwnerIncome ownerIncome = new OwnerIncome().setOwnerIncomeId(ownerParam.getOwnerId())
                .setOwnerIncomeMoney(vip.getOwnerVipPrice().negate())
                .setOwnerIncomeId(owner.getOwnerId())
                .setOwnerIncomeTime(TimeUtils.now());
        int insert = ownerIncomeMapper.insert(ownerIncome);

        if (insert > 0)
            return "开通成功";
        return "失败";
    }

    @Override
    public String updateOwnerMoney(OwnerParam ownerParam) {

        Owner owner = ownerMapper.selectById(ownerParam.getOwnerId());
        owner.setOwnerMoney(ownerParam.getOwnerMoney());

        int update = ownerMapper.update(owner,
                new UpdateWrapper<Owner>()
                        .eq("owner_id", owner.getOwnerId())
                        .eq("owner_money", owner.getOwnerMoney()));
        if (update > 0) {
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    public void deleteOwnerVip() {
        ownerMapper.updateOwnerVip();
    }
}
